/*
 * Copyright 2010 Christoph Widulle (christoph.widulle@googlemail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
 * License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS"
 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */


package com.android1.amarena2d.input;

import com.android1.amarena2d.engine.EngineObject;


/**
 * Will detect a position independent finger scroll on the screen and will update the given position
 */
public abstract class ScrollDetector extends EngineObject implements TouchListener {

    boolean inverseX;
    boolean inverseY;
    float scale = 1F;
    boolean exclusive = true;

    float lastDragX;
    float lastDragY;

    int activePointer;
    boolean hit;


    public ScrollDetector( final boolean inverseX, final boolean inverseY) {
        this.inverseX = inverseX;
        this.inverseY = inverseY;
    }

    public ScrollDetector( final float scale) {
        this.scale = scale;
    }

    public ScrollDetector( final float scale, final boolean inverseX, final boolean inverseY) {
        this.scale = scale;
        this.inverseX = inverseX;
        this.inverseY = inverseY;
    }



    public boolean isExclusive() {
        return exclusive;
    }

    /**
     * If true, it will consume the event and no other touchlistener will be notified.
     * Set to false, if you want to have more draggable layers for example.
     *
     * @param exclusive
     * @return
     */
    public ScrollDetector setExclusive(final boolean exclusive) {
        this.exclusive = exclusive;
        return this;
    }

    public ScrollDetector setScale(final float scale) {
        this.scale = scale;
        return this;
    }

    public abstract void onScroll(float diffx, float diffy, int pointer);

    @Override
    public boolean onTouchDown(float x, float y, int pointer, boolean isHit) {
        if (!isHit || (this.activePointer != -1 && this.activePointer != pointer))
            return false;

        lastDragX = x;
        lastDragY = y;
        this.hit = true;
        this.activePointer = pointer;
        return exclusive;
    }

    @Override
    public boolean onTouchUp(float x, float y, int pointer, boolean isHit) {
        if (this.activePointer != -1 && this.activePointer != pointer)
            return false;
        this.hit = false;
        this.activePointer = -1;
        return exclusive;
    }

    @Override
    public boolean onTouchDragged(float x, float y, int pointer, boolean isHit) {
        if (!hit  || (this.activePointer != -1 && this.activePointer != pointer))
            return false;
        float diffx = (x - lastDragX) * scale;
        float diffy = (y - lastDragY) * scale;

        lastDragX = x;
        lastDragY = y;

        if (inverseX)
            diffx *= -1;
        if (inverseY)
            diffy *= -1;

        onScroll(diffx, diffy,this.activePointer);

        return exclusive;

    }
}
